//
//  NRWCarTabbarController.m
//  New_Runway
//
//  Created by mack on 2018/1/22.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarTabbarController.h"
#import "UIView+DDAddition.h"
#import "NRWCarTabbarView.h"
#import "NRWCarRootViewController.h"
#import "NRWCarLoginModel.h"

#import "NRWCarHomeController.h"
#import "NRWCarBuyCarsController.h"
#import "NRWCarWorkDeskController.h"
#import "NRWCarMineController.h"

@interface NRWCarTabbarController ()
@property(nonatomic, strong) UIView *footTabbarView;
@property(nonatomic, assign) NSInteger selectViewNumber;
@property(nonatomic, strong) NSMutableArray *tabbatItemArr;
@property(nonatomic, strong) NRWCarTabbarView *tabbarItem;
@property (nonatomic, strong) NSArray *resourceList;
@property (assign, nonatomic) CGFloat y;
@property (nonatomic,strong) NRWCarLoginModel *loginModel;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation NRWCarTabbarController
static NSInteger ranN ;
+ (NRWCarTabbarController *)sharedTabBarVC {
    static NRWCarTabbarController *tabBarVC = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
        tabBarVC = [[NRWCarTabbarController alloc] init];

// });

    return tabBarVC;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.hidden = YES;
    _y = height_tabbar;
    [self loadData];

    
}
-(void)LoginSuccessful:(NSNotification *)noti{
    NRWCarUser *model= [noti object];
    self.resourceList = model.resourceList;
    for (NSDictionary *dic in self.resourceList) {
        NRWCarLoginModel *loginModel = [[NRWCarLoginModel alloc] init];
        [loginModel setValuesForKeysWithDictionary:dic];
        [self.dataArray addObject:loginModel];
    }
    ranN = self.dataArray.count;
    [self setupUI];

}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];

    
}
-(void)loadData{
    self.resourceList = [NRWCarUser sharedUser].resourceList;
    for (NSDictionary *dic in self.resourceList) {
        NRWCarLoginModel *loginModel = [[NRWCarLoginModel alloc] init];
        [loginModel setValuesForKeysWithDictionary:dic];
        [self.dataArray addObject:loginModel];
    }
    ranN = self.dataArray.count;
    [self setupUI];
}

-(void)setupUI{
    
    _footTabbarView = [[UIView alloc]initWithFrame:CGRectMake(0, Screen_height - _y , Screen_width, _y)];
    
    _footTabbarView.backgroundColor = MAIN_BAR_COLOR();
    [self.view addSubview:_footTabbarView];
    
    _lineView = [UIView lineViewForColor:[UIColor colorFromHexCode:@"#cccccc"] AndFrame:CGRectMake(0, 0, Screen_width, 0.5)];
    [_footTabbarView addSubview:_lineView];
    
    _selectViewNumber = 0;
    _tabbatItemArr = [NSMutableArray new];
    
    for (int i = 0 ; i < ranN; i++) {
        NSString *_title;
        UIImage *_image;
        UIImage *_highlightedImge;
        //        switch (i) {
        //            case 0:
        //                _image = [UIImage imageNamed:@"首页icon"];
        //                _highlightedImge = [UIImage imageNamed:@"首页选中icon"];
        //                break;
        //            case 1:
        //                _image = [UIImage imageNamed:@"晒物icon"];
        //                _highlightedImge = [UIImage imageNamed:@"晒物选中icon"];
        //
        //                break;
        //
        //            case 2:
        //                _image = [UIImage imageNamed:@"商城icon"];
        //                _highlightedImge = [UIImage imageNamed:@"商城选中icon"];
        //
        //                break;
        //
        //            case 3:
        //                _image = [UIImage imageNamed:@"我的icon"];
        //                _highlightedImge = [UIImage imageNamed:@"我的选中icon"];
        //
        //                break;
        //            default:
        //                break;
        //        }
        
        
        
        
        switch (i) {
            case 0:
          
                _loginModel = self.dataArray[0];
                if ([_loginModel.resourceIcon isEqual:@"home"]) {
                    _image = [UIImage imageNamed:@"Tab_Home"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_Home_sel"];
                    _title = @"首页";
                }else if ([_loginModel.resourceIcon isEqual:@"cars"]){
                    _image = [UIImage imageNamed:@"Tab_car"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_car_sel"];
                    _title = @"买车";
                    
                }else if ([_loginModel.resourceIcon isEqual:@"desk"]){
                    
                    _image = [UIImage imageNamed:@"Tab_workdesk"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_workdesk_sel"];
                    _title = @"工作台";
                }else if ([_loginModel.resourceIcon isEqual:@"user"]){
                    
                    _image = [UIImage imageNamed:@"Tab_Mine"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_Mine_sel"];
                    _title = @"我的";
                }
               
                break;
            case 1:
                _loginModel = self.dataArray[1];
                if ([_loginModel.resourceIcon isEqual:@"home"]) {
                    _image = [UIImage imageNamed:@"Tab_Home"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_Home_sel"];
                    _title = @"首页";
                }else if ([_loginModel.resourceIcon isEqual:@"cars"]){
                    _image = [UIImage imageNamed:@"Tab_car"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_car_sel"];
                    _title = @"买车";
                    
                }else if ([_loginModel.resourceIcon isEqual:@"desk"]){
                    
                    _image = [UIImage imageNamed:@"Tab_workdesk"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_workdesk_sel"];
                    _title = @"工作台";
                }else if ([_loginModel.resourceIcon isEqual:@"user"]){
                    
                    _image = [UIImage imageNamed:@"Tab_Mine"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_Mine_sel"];
                    _title = @"我的";
                }
                
                
                break;
                
            case 2:
                _loginModel = self.dataArray[2];
                if ([_loginModel.resourceIcon isEqual:@"home"]) {
                    _image = [UIImage imageNamed:@"Tab_Home"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_Home_sel"];
                    _title = @"首页";
                }else if ([_loginModel.resourceIcon isEqual:@"cars"]){
                    _image = [UIImage imageNamed:@"Tab_car"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_car_sel"];
                    _title = @"买车";
                    
                }else if ([_loginModel.resourceIcon isEqual:@"desk"]){
                    
                    _image = [UIImage imageNamed:@"Tab_workdesk"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_workdesk_sel"];
                    _title = @"工作台";
                }else if ([_loginModel.resourceIcon isEqual:@"user"]){
                    
                    _image = [UIImage imageNamed:@"Tab_Mine"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_Mine_sel"];
                    _title = @"我的";
                }
                
                
                break;
                
            case 3:
                _loginModel = self.dataArray[3];
                if ([_loginModel.resourceIcon isEqual:@"home"]) {
                    _image = [UIImage imageNamed:@"Tab_Home"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_Home_sel"];
                    _title = @"首页";
                }else if ([_loginModel.resourceIcon isEqual:@"cars"]){
                    _image = [UIImage imageNamed:@"Tab_car"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_car_sel"];
                    _title = @"买车";
                    
                }else if ([_loginModel.resourceIcon isEqual:@"desk"]){
                    
                    _image = [UIImage imageNamed:@"Tab_workdesk"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_workdesk_sel"];
                    _title = @"工作台";
                }else if ([_loginModel.resourceIcon isEqual:@"user"]){
                    
                    _image = [UIImage imageNamed:@"Tab_Mine"];
                    _highlightedImge = [UIImage imageNamed:@"Tab_Mine_sel"];
                    _title = @"我的";
                }
                
                
                break;
            default:
                break;
        }
        
        //        YHZXTabBarView *tabbarItem = [[YHZXTabBarView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4 *i ,2+ self.view.height - 49, SCREEN_WIDTH/4, 49) Image:_image highlightedImge:_highlightedImge];
        
        _tabbarItem = [[NRWCarTabbarView alloc]initWithFrame:CGRectMake(Screen_width/ranN *i ,2+ self.view.height - _y, Screen_width/ranN, 49) Image:_image highlightedImge:_highlightedImge Title:_title];
        
        _tabbarItem.tag = i;
        [_tabbatItemArr addObject:_tabbarItem];
        if (i == 0) {
            [_tabbarItem setBtnSelect:YES];
        }
        __weak NRWCarTabbarController *wSelf = self;
        [_tabbarItem setSelectFootTabbarBtn:^(int footTabbarBtnSelfTag) {
            
            [wSelf pressTabbarSelect:footTabbarBtnSelfTag];
            
        }];
        [self.view addSubview:_tabbarItem];
        
        
        
    }
    [self pressTabbarSelect:(int)_selectViewNumber];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self selector:@selector(jumpAppointPage:) name:@"JUMP" object:nil];
    //        [center addObserver:self selector:@selector(login:) name:@"LOGIN" object:nil];
}
-(void)jumpAppointPage:(NSNotification *)note{
    
    [self pressTabbarSelect:[note.object intValue]];
    for (NRWCarTabbarView *_fView in _tabbatItemArr) {
        if ([note.object intValue] == _fView.tag) {
            [_fView setBtnSelect:YES];
        }
        
        
    }
}

- (void)pressTabbarSelect:(int)footTabbarView
{
    
    if (ranN == 1) {
        if (!self.viewControllers) {
            self.viewControllers = @[[self viewControllerWithTabtag:0]];
        }
    }else if (ranN == 2){
        if (!self.viewControllers) {
            self.viewControllers = @[[self viewControllerWithTabtag:0],[self viewControllerWithTabtag:1]];
        }
    }else if (ranN == 3){
        if (!self.viewControllers) {
            self.viewControllers = @[[self viewControllerWithTabtag:0],[self viewControllerWithTabtag:1],[self viewControllerWithTabtag:2]];
        }
    }else if (ranN == 4){
        if (!self.viewControllers) {
            self.viewControllers = @[[self viewControllerWithTabtag:0],[self viewControllerWithTabtag:1],[self viewControllerWithTabtag:2],[self viewControllerWithTabtag:3]];
        }
    }
   
    if (_selectViewNumber == footTabbarView) {
        [self btnHead];
    }else{
        _selectViewNumber = footTabbarView;
        
        NRWCarRootViewController *_bvc = [self.viewControllers objectAtIndex:_selectViewNumber];
        if ([_bvc respondsToSelector:@selector(mainTabBarFirstSelect)]) {
            [_bvc mainTabBarFirstSelect];
        }
        self.selectedIndex = footTabbarView;
    }
    
    for (NRWCarTabbarView *_fView in _tabbatItemArr) {
        if (footTabbarView != _fView.tag) {
            [_fView setBtnSelect:NO];
        }
    }
    
}

-(UIViewController*)viewControllerWithTabtag:(NSInteger)tag{
    
    UIViewController *_viewController;
    switch (tag) {
        case 0:
        {
            
            _loginModel = self.dataArray[0];
         _viewController =   [self chooseVC:_viewController loginModel:_loginModel];

            
        }
            break;
            
        case 1:
        {
            
            _loginModel = self.dataArray[1];
          _viewController =  [self chooseVC:_viewController loginModel:_loginModel];

        }
            break;
            
        case 2:
        {
            _loginModel = self.dataArray[2];
         _viewController =   [self chooseVC:_viewController loginModel:_loginModel];

            
        }
            break;
            
        case 3:
        {
           
            _loginModel = self.dataArray[3];
          _viewController =  [self chooseVC:_viewController loginModel:_loginModel];
        }
            break;
            
        default:
            break;
    }
    
    return _viewController;
    
    
}

-(UIViewController *)chooseVC:(UIViewController *)viewController loginModel:(NRWCarLoginModel *)model{
    
    if ([model.resourceIcon isEqual:@"home"]) {
        
        
        viewController = [[NRWCarHomeController alloc] init];
        
    }else if ([model.resourceIcon isEqual:@"cars"]){
        
        
        viewController = [[NRWCarBuyCarsController alloc]init];
        
        
    }else if ([model.resourceIcon isEqual:@"desk"]){
        
       NRWCarWorkDeskController *deskVC = [[NRWCarWorkDeskController alloc] init];
        deskVC.loginModel = model;
        viewController = deskVC;
        
        
    }else if ([model.resourceIcon isEqual:@"user"]){
      NRWCarMineController *mineVC = [[NRWCarMineController alloc] init];
        mineVC.loginModel = model;
        viewController = mineVC;
        
    }
    return viewController;
}

-(void)btnHead{
    NRWCarRootViewController *_vc = (NRWCarRootViewController *)self.selectedViewController;
    if ([_vc respondsToSelector:@selector(mainTabBarSelectAgain)]) {
        [_vc mainTabBarSelectAgain];
    }
}
-(UIImage *)drawTabBarItemBackGroundImageWithSize:(CGSize)size{
    
    UIGraphicsBeginImageContext(size);
    CGContextRef ctf = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(ctf, 92.0/255, 132.0/255, 241.0/255, 1);
    CGContextFillRect(ctf, CGRectMake(0, 0, size.width, size.height));
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

- (BOOL)shouldAutorotate {
    
    return NO;
    
}

// viewcontroller支持哪些转屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    return [self.selectedViewController supportedInterfaceOrientations];
}

-(void)showRedPonit:(NSInteger)index{
    if (_tabbarItem.tag == index) {
        _tabbarItem.redPoint.hidden = NO;
        
    }
    
}
-(void)hidenRedPoint:(NSInteger)index{
    if (_tabbarItem.tag == index) {
        _tabbarItem.redPoint.hidden = YES;
        
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
